package com.yc.service;

import com.google.gson.Gson;
import com.yc.bean.MessageBean;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author zwei
 * @create 2024-08-11 16:25
 */
@Component  // 注入到spring容器中
@Log4j
public class JmsMessageProducer {
    @Value("${queueName}")
    private String queueName;
    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(MessageBean message) {
        //将bean转成一个json字符串   序列化到 activeMQ
        Gson gson = new Gson();
        String json = gson.toJson(message);
        log.debug("发送的消息为：" + json);
        jmsTemplate.convertAndSend(queueName, json);    //发送消息到队列
    }
}
